package com.shaoshaohuo.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.AddNewAddressEntity;
import com.shaoshaohuo.app.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 检索地址
 * Created by Administrator on 2017/6/12.
 */

public class AdderssPOIActivity extends BaseActivity implements GeocodeSearch.OnGeocodeSearchListener, Inputtips.InputtipsListener {

    private static final String TAG = "TAG";
    private GeocodeSearch geocoderSearch;
    private EditText editText;
    private ListView adders_list;
    private List<Tip> list;
    private List<String> strlist = new ArrayList<>();
    private ArrayAdapter<String> stringArrayAdapter;
    private String city = "";
    private String district;
    private String intent_city;
    private String province;

    private double latitude;
    private double longitude;
    private String address;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_serach);
        Intent intent = getIntent();
        city = intent.getStringExtra("city");
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if (city == null) {
            city = "";
        }
        geocoderSearch = new GeocodeSearch(this);
        geocoderSearch.setOnGeocodeSearchListener(this);
        adders_list = (ListView) findViewById(R.id.adders_List);
        stringArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strlist);
        adders_list.setAdapter(stringArrayAdapter);
        changeTransltestaus(((ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content)).getChildAt(0), this, TransltestausModel.NoePicter, getResources().getColor(R.color.color_49b152));
        adders_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {


            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (list != null) {
                    LatLonPoint point = list.get(position).getPoint();
                    district = list.get(position).getDistrict();
                    latitude = point.getLatitude();
                    longitude = point.getLongitude();
//                    ToastUtil.toast("经度为" + longitude + "纬度" + latitude);
                    address = list.get(position).getAddress();

                    if (address.length() > 0) {
                        RegeocodeQuery regeocodeQuery = new RegeocodeQuery(point, 200, GeocodeSearch.AMAP);
                        geocoderSearch.getFromLocationAsyn(regeocodeQuery);
                        startLoadingDialog();
                    } else {
                        ToastUtil.toast("地址不存在");
                    }

//                    AddNewAddressEntity
                }
            }
        });
        editText = (EditText) findViewById(R.id.edt);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String trim = editText.getText().toString().trim();
                InputtipsQuery inputquery = new InputtipsQuery(city + trim, city);
                Inputtips inputTips = new Inputtips(AdderssPOIActivity.this.getApplicationContext(), inputquery);
                inputTips.setInputtipsListener(AdderssPOIActivity.this);
                inputTips.requestInputtipsAsyn();

            }
        });

//        new GeocodeQuery()


    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        dismissLoadingDialog();
        RegeocodeAddress regeocodeAddress = regeocodeResult.getRegeocodeAddress();
//        List<GeocodeAddress> geocodeAddressList = regeocodeResult.getGeocodeAddressList();
//        for(int index=0;index<geocodeAddressList.size();index++){
//            GeocodeAddress geocodeAddress = geocodeAddressList.get(index);
//            LatLonPoint latLonPoint = geocodeAddress.getLatLonPoint();
//            if(latLonPoint.getLatitude()==latitude&&latLonPoint.getLongitude()==longitude){
        Intent intent = new Intent();
        AddNewAddressEntity addNewAddressEntity = new AddNewAddressEntity();
        addNewAddressEntity.setAddressname(address);
        addNewAddressEntity.setLatitude(latitude + "");
        addNewAddressEntity.setLongitude(longitude + "");
        addNewAddressEntity.setDistrict(regeocodeAddress.getDistrict());
        addNewAddressEntity.setProvince(regeocodeAddress.getProvince());
        addNewAddressEntity.setIntent_city(regeocodeAddress.getCity());
        intent.putExtra("address", addNewAddressEntity);
        setResult(2, intent);
        finish();
//            }

    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {


    }

    @Override
    public void onGetInputtips(List<Tip> list, int rCode) {
        if (rCode == 1000) {// 正确返回
            this.list = list;
            strlist.clear();
            for (int i = 0; i < list.size(); i++) {
                String address = list.get(i).getAddress();
                if (address.length() > 0) {
                    strlist.add(address);
                }

                stringArrayAdapter.notifyDataSetChanged();
                Log.e(TAG, "onGetInputtips: " + address);
            }
        }
    }
}
